class Solution
{
public:
    int splitArray(vector<int> &nums, int k)
    {
        int n = nums.size();
        vector<int> prefixSum(n + 1, 0);
        for (int i = 0; i < n; i++)
        {
            prefixSum[i + 1] = prefixSum[i] + nums[i];
        }

        vector<vector<int>> dp(n, vector<int>(k, INT_MAX));
        for (int i = 0; i < n; i++)
        {
            dp[i][0] = prefixSum[i + 1];
        }

        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < k; j++)
            {
                for (int l = 0; l < i; l++)
                {
                    dp[i][j] = min(dp[i][j], max(dp[l][j - 1], prefixSum[i + 1] - prefixSum[l + 1]));
                }
            }
        }

        return dp[n - 1][k - 1];
    }
};